<?php


namespace App\Models;


use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;

class ImFriendsCirclePraise extends BaseModel
{
    protected $table = 'im_friends_circle_praise';

    //点赞者信息
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'friend_id', 'id')
            ->select(['id', 'nickname', 'headimg']);
    }

    /**
     * @desc 朋友圈点赞
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addCircleLike($input, $userId = 0)
    {
        $imFriendsCircle = new ImFriendsCircle();
        $imFriendsCircle = $imFriendsCircle->where(['id'=>$input['fc_id']])->first();
        if (!$imFriendsCircle)
        {
            return ['code' => 1, 'msg' => '朋友圈已被删'];
        }

        // 查询好友是否好友关系
        $imUserFriend = new ImUserFriend();
        $imUserFriend = $imUserFriend->where(['user_id'=>$userId, 'other_uid'=>$imFriendsCircle['user_id'], 'circle_status'=>1, 'status'=>1])->first();
        if (!$imUserFriend && ($userId != $imFriendsCircle['user_id']))
        {
            return ['code' => 1, 'msg' => '非好友不能点赞'];
        }

        $friendsCirclePraise = $this->where(['fc_id'=>$input['fc_id'], 'friend_id'=>$userId])->first();

        // 点赞
        if ($input['type'] == 1)
        {
            if (!$friendsCirclePraise)
            {
                $this->fc_id = $input['fc_id'];
                $this->friend_id = $userId;
                $this->status = 1;
                $this->save();

                $imFriendsCircle->increment('like_count');
            }
            else
            {
                $friendsCirclePraise->status = 1;
                $friendsCirclePraise->save();

                $imFriendsCircle->increment('like_count');
            }

            // 点赞插入到消息表
            if (empty($friendsCirclePraise) && ($imFriendsCircle['user_id'] != $userId))
            {
                $ImNotification = new ImNotification();
                $ImNotification->addMessage($imFriendsCircle['user_id'], 1, $userId, $imFriendsCircle['id'], '点赞');
            }

            return ['code' => 0, 'msg' => '点赞成功'];
        }
        else
        {

            // 取消点赞
            if (!$friendsCirclePraise)
            {
                $this->fc_id = $input['fc_id'];
                $this->friend_id = $userId;
                $this->status = 0;
                $this->save();

                $imFriendsCircle->decrement('like_count');

                return ['code' => 0, 'msg' => '取消成功'];
            }
            else
            {
                $friendsCirclePraise->status = 0;
                $friendsCirclePraise->save();

                $imFriendsCircle->decrement('like_count');

                return ['code' => 0, 'msg' => '取消成功'];
            }
        }
    }

    /**
     * 通过uid获取自己点赞的朋友圈的id
     * @param array $fileIds
     * @return array
     */
    public function getCirclePraiseByuid($uid)
    {
        if (empty($uid))
        {
            return [];
        }

        $query = $this->where(['status'=>1, 'friend_id'=>$uid]);
        $list = $query->select('id','fc_id')
            ->get()->toArray();
        if ($list)
        {
            return $list;
        }

        return [];
    }
}
